//
//  HomeGameCell.swift
//  d11
//
//  Created by peale_pro on 2019/6/18.
//  Copyright © 2019年 Tech_001. All rights reserved.
//

import UIKit
typealias GameSelectBlock2 = (_ dict: [String:Any]?,_ index:Int) -> Void
class HomeGameCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
    var gameSelectBlock2: GameSelectBlock2?
    func callClickBlock(_ block: @escaping GameSelectBlock2) {
        gameSelectBlock2 = block
    }
    var arrayList : [[String:Any]]? //数据列表
    var gameCount :Int = 1
    var selectIndex = 0
    lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        //        if kScreenWidth<=320{
        //            layout.itemSize = CGSize(width: (kScreenWidth-13-80)/4, height: ((kScreenWidth-13-80)/4)+45)
        //            layout.sectionInset = UIEdgeInsets.init(top: 20, left: 10, bottom: 0, right: 10)
        //            layout.minimumInteritemSpacing = 20        //        }else{
        if self.gameCount == 1{
              layout.itemSize = CGSize(width: ((kScreenWidth-20) / CGFloat(self.gameCount)), height: 160)
             layout.sectionInset = UIEdgeInsets.init(top: 20, left: 10, bottom: 20, right: 0)
        }else{
              layout.itemSize = CGSize(width: ((kScreenWidth-50) / CGFloat(self.gameCount)), height: 160)
             layout.sectionInset = UIEdgeInsets.init(top: 20, left: 10, bottom: 20, right: 10)
        }
       
      //  layout.minimumInteritemSpacing = 10
        //  }
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .horizontal
        let collView = UICollectionView.init(frame: CGRect(x:0, y:0, width:kScreenWidth, height:200), collectionViewLayout: layout)
        
        if collView.responds(to:#selector(setter: UIView.layoutMargins)) {
            collView.layoutMargins = .zero;
        }
        
        collView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth.union(UIView.AutoresizingMask.flexibleHeight)
        collView.register(HomeGameItemCell.self, forCellWithReuseIdentifier: "HomeGameItemCell")
        //        collView.register(HelperListHeadView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HelperListHeadView1")
        collView.showsVerticalScrollIndicator = false
        collView.showsHorizontalScrollIndicator = false
        collView.backgroundColor = HexRGBAlpha(0xf6f5fa, 1)
        collView.delegate = self
        collView.dataSource = self
        // collView.backgroundColor = UIColor.red
        return collView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        if reuseIdentifier == "HomeGameCell1"{
            self.gameCount = 1
        }else{
            self.gameCount = 2
        }
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor = HexRGBAlpha(0xf6f5fa, 1)
        self.contentView.backgroundColor = HexRGBAlpha(0xf6f5fa, 1)
        self.contentView.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(200)
            make.left.equalToSuperview()
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayList?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        return UICollectionReusableView.init()
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:HomeGameItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeGameItemCell", for: indexPath) as! HomeGameItemCell
        let dict:[String:Any] = self.arrayList?[indexPath.row] ?? [:]
        cell.setData(imgUrl: dict["imgNameIOS"] as? String ?? "",gameId:dict["id"] as? String ?? "",gameCount: self.gameCount)
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        if gameSelectBlock2 != nil {
            gameSelectBlock2!(self.arrayList?[indexPath.row],indexPath.row)
        }
    }
   
    func setDataWithRow(row:Int,gameCount:Int) -> Void{
        self.gameCount = gameCount
        self.arrayList = GlobalInfo.platformInfoData?[row]["game_list"] as? [[String : Any]]
        self.collectionView.reloadData()
    }
    func starAnimationWithRow(row:Int,gameCount:Int) -> Void{
        self.gameCount = gameCount
        self.arrayList = GlobalInfo.platformInfoData?[row]["game_list"] as? [[String : Any]]
        self.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        self.collectionView.reloadData()
         self.collectionView.layoutIfNeeded()
       // self.collectionView.layoutSubviews()
        TableViewAnimationKit.toTopAnimation(with: self.collectionView)
//        let cells:NSArray = self.collectionView.visibleCells as NSArray
//        for itemIndex in 0..<cells.count{
//            let cell :UICollectionViewCell = cells.object(at: itemIndex) as! UICollectionViewCell
//            cell.alpha = 0.0
//            cell.transform = CGAffineTransform.init(scaleX: cell.frame.size.width/2, y: 0)
//            UIView.animate(withDuration: TimeInterval(0.6), delay: TimeInterval(CGFloat(itemIndex)*(0.6/CGFloat(cells.count))), usingSpringWithDamping: 0.8, initialSpringVelocity: 0.1, options: UIView.AnimationOptions.curveEaseOut, animations: {
//                cell.transform = CGAffineTransform.identity
//                cell.alpha = 0.99
//            }) { (finished) in
//                cell.alpha = 1.0
//            }
//        }
    }
    
}
